// Decompiled by Jad v1.5.7g. Copyright 2000 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/SiliconValley/Bridge/8617/jad.html
// Decompiler options: packimports(3) fieldsfirst ansi
// Source File Name: FormattedProperties.java

package com.hd123.rumba.util;

import java.io.*;
import java.util.*;

public class FormattedProperties extends Properties {
	class Line {

		Line() {
		  super();
		}
	}

	class EmptyLine extends Line {

		EmptyLine() {
			super();
		}
	}

	class CommentLine extends Line {

		public String comment;

		public CommentLine(String s) {
			super();
			comment = s;
		}
	}

	class PropertyLine extends Line {

		public String key;

		public String value;

		public PropertyLine(String s, String s1) {
			super();
			key = s;
			value = s1;
		}
	}

	private static final char hexDigit[] = { '0', '1', '2', '3', '4', '5', '6',
			'7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };

	private Vector lines;

	private boolean continueLine(String s) {
		int i = 0;
		for (int j = s.length() - 1; j >= 0 && s.charAt(j--) == '\\';)
			i++;

		return i % 2 == 1;
	}

	public synchronized void load(InputStream inputstream) throws IOException {
		BufferedReader bufferedreader = null;
		bufferedreader = new BufferedReader(new InputStreamReader(inputstream,
				"8859_1"));
		do {
			String s = bufferedreader.readLine();
			if (s == null)
				return;
			if (s.trim().length() <= 0) {
				lines.add(new EmptyLine());
			} else {
				char c = s.charAt(0);
				if (c == '#' || c == '!' || s.indexOf(61) == -1) {
					lines.add(new CommentLine(s));
				} else {
					String s1;
					String s2;
					for (; continueLine(s); s = new String(s2 + s1)) {
						s1 = bufferedreader.readLine();
						if (s1 == null)
							s1 = new String("");
						s2 = s.substring(0, s.length() - 1);
						int i = 0;
						for (i = 0; i < s1.length(); i++)
							if (" \t\r\n\f".indexOf(s1.charAt(i)) == -1)
								break;

						s1 = s1.substring(i, s1.length());
					}

					int j = s.length();
					int k;
					for (k = 0; k < j; k++)
						if (" \t\r\n\f".indexOf(s.charAt(k)) == -1)
							break;

					int l;
					for (l = k; l < j; l++) {
						char c1 = s.charAt(l);
						if (c1 == '\\') {
							l++;
							continue;
						}
						if ("=: \t\r\n\f".indexOf(c1) != -1)
							break;
					}

					int i1;
					for (i1 = l; i1 < j; i1++)
						if (" \t\r\n\f".indexOf(s.charAt(i1)) == -1)
							break;

					if (i1 < j && "=:".indexOf(s.charAt(i1)) != -1)
						i1++;
					for (; i1 < j; i1++)
						if (" \t\r\n\f".indexOf(s.charAt(i1)) == -1)
							break;

					String s3 = s.substring(k, l);
					String s4 = l < j ? s.substring(i1, j) : "";
					s3 = loadConvert(s3);
					s4 = loadConvert(s4);
					put(s3, s4);
				}
			}
		} while (true);
	}

	private String loadConvert(String s) {
		int i = 0;
		StringBuffer stringbuffer = null;
		i = s.length();
		stringbuffer = new StringBuffer(i);
		for (int j = 0; j < i;) {
			char c = s.charAt(j++);
			if (c == '\\') {
				c = s.charAt(j++);
				if (c == 'u') {
					int k = 0;
					for (int l = 0; l < 4; l++) {
						c = s.charAt(j++);
						if (c >= '0' && c <= '9')
							k = ((k << 4) + c) - 48;
						else if (c >= 'a' && c <= 'f')
							k = ((k << 4) + 10 + c) - 97;
						else if (c >= 'A' && c <= 'F')
							k = ((k << 4) + 10 + c) - 65;
						else
							throw new IllegalArgumentException(
									"Malformed \\uxxxx encoding.");
					}

					stringbuffer.append((char) k);
				} else if (c == 't')
					stringbuffer.append('\t');
				else if (c == 'r')
					stringbuffer.append('\r');
				else if (c == 'n')
					stringbuffer.append('\n');
				else if (c == 'f')
					stringbuffer.append('\f');
				else if (c == '\\')
					stringbuffer.append('\\');
			} else {
				stringbuffer.append(c);
			}
		}

		return stringbuffer.toString();
	}

	public synchronized Object put(String s, String s1) {
		Object obj = null;
		obj = super.put(s, s1);
		for (int i = 0; i < lines.size(); i++) {
			Line line = (Line) lines.elementAt(i);
			if (line instanceof PropertyLine) {
				PropertyLine propertyline = (PropertyLine) line;
				if (propertyline.key.equals(s)) {
					propertyline.value = s1;
					return obj;
				}
			}
		}

		lines.add(new PropertyLine(s, s1));
		return obj;
	}

	public synchronized Object setProperty(String s, String s1) {
		return put(s, s1);
	}

	public synchronized void store(OutputStream outputstream, String s)
			throws IOException {
		BufferedWriter bufferedwriter = null;
		bufferedwriter = new BufferedWriter(new OutputStreamWriter(
				outputstream, "8859_1"));
		if (s != null) {
			writeln(bufferedwriter, "# " + s);
			writeln(bufferedwriter, "# " + (new Date()).toString());
			writeln(bufferedwriter, "");
		}
		for (int i = 0; i < lines.size(); i++) {
			Line line = (Line) lines.elementAt(i);
			if (line instanceof PropertyLine) {
				PropertyLine propertyline = (PropertyLine) line;
				writeln(bufferedwriter, storeConvert(propertyline.key) + "="
						+ storeConvert(propertyline.value));
			} else if (line instanceof CommentLine) {
				CommentLine commentline = (CommentLine) line;
				writeln(bufferedwriter, commentline.comment);
			} else if (line instanceof EmptyLine)
				writeln(bufferedwriter, "");
		}

		bufferedwriter.flush();
	}

	private String storeConvert(String s) {
		int i = 0;
		StringBuffer stringbuffer = null;
		i = s.length();
		stringbuffer = new StringBuffer(i * 2);
		for (int j = 0; j < i;) {
			char c = s.charAt(j++);
			switch (c) {
			case 92: // '\\'
				stringbuffer.append('\\');
				stringbuffer.append('\\');
				break;

			case 9: // '\t'
				stringbuffer.append('\\');
				stringbuffer.append('t');
				break;

			case 10: // '\n'
				stringbuffer.append('\\');
				stringbuffer.append('n');
				break;

			case 13: // '\r'
				stringbuffer.append('\\');
				stringbuffer.append('r');
				break;

			case 12: // '\f'
				stringbuffer.append('\\');
				stringbuffer.append('f');
				break;

			default:
				if (c < '\024' || c > '\177') {
					stringbuffer.append('\\');
					stringbuffer.append('u');
					stringbuffer.append(toHex(c >> 12 & 0xf));
					stringbuffer.append(toHex(c >> 8 & 0xf));
					stringbuffer.append(toHex(c >> 4 & 0xf));
					stringbuffer.append(toHex(c & 0xf));
					break;
				}
				if ("\t\r\n\f#!".indexOf(c) != -1)
					stringbuffer.append('\\');
				stringbuffer.append(c);
				break;
			}
		}

		return stringbuffer.toString();
	}

	public synchronized Object remove(Object obj) {
		Object obj1 = super.remove(obj);
		if (obj1 != null) {
			for (int i = 0; i < lines.size(); i++) {
				Line line = (Line) lines.elementAt(i);
				if (!(line instanceof PropertyLine))
					continue;
				PropertyLine propertyline = (PropertyLine) line;
				if (!propertyline.key.equals(obj))
					continue;
				lines.removeElementAt(i);
				break;
			}

		}
		return obj1;
	}

	private static char toHex(int i) {
		return hexDigit[i & 0xf];
	}

	private static void writeln(BufferedWriter bufferedwriter, String s)
			throws IOException {
		bufferedwriter.write(s);
		bufferedwriter.newLine();
	}

	public FormattedProperties() {
		lines = new Vector();
	}

}
